require("../util.js");
require("../component/components.js");
require("../../jsMt/util.js");
var userInfo=JSON.parse(sessionStorage.getItem("userInfo"));
var shopInfo=JSON.parse(sessionStorage.getItem("shopData"));
if(!userInfo){
    location.href="/loginIndex"
}
// var initData=JSON.parse(sessionStorage.getItem(""));
//console.log(shopInfo.mediaAreas)
var vm = new Vue({
    el:"#newTemplate",
    data:{
        isSystem:"",
        freightId:"",
        freightList:{},
        isScreen:"",
        type:"",
        shopData:shopInfo.mediaAreas,
        areas:[],
        checked1:[],
        checked2:[],
        checked3:[],
        checked4:[],
        checked0:[],
        areas1:[],
        index:"",
        // initId1:"",
        // initId1Name:"",
        idList:[], //当前选中id
        idList1:[],  //大分类
        idList2:"",  //已选大分类id
        idList3:[],
        disIdList:[], //禁用大分类id
        checkedList:[],
        // initId2:"",
        areafreightId:"",//当前要删除 子运费id
        areasList:[],//所有id
        areasList1:"",//已选id
        disableList:[],//禁用id
        optionalList:[],//可选
        pop:{
            question: "确认删除此地区运费？",
            show: false
        },

        checkedNames:[], //当前所选中的id
        provinceName:"",
        formatNum:0,
        // addressId:"",
        // addressList:{},
        list:{
            loginUid:userInfo.id,
            name:"", //名称
            isExcept:"", //1除以下城市之外 0否
            provinceId :"", //省份id 逗号隔开
            freightId:"", //运费id 没有值就是增加 有值就是修改
            piece:"",  //件
            money:"",  //价格
            addPiece:"",  //增加件
            addMoney :"", //增加价格
            areafreightId:[], //子运费id数组
            subprovinceId:[],  //省份id数组 逗号隔开
            subpiece:[],  //件数组
            submoney:[],  //价格数组
            subaddPiece:[], //增加件数组
            subaddMoney :[] //增加价格数组
        }
    },
    mounted:function(){
        this.init();
    },
    methods:{
        //后退
        getBack: function () {
            location.href="/freightTemplate";
        },
        bgimg:function(url){
            url = url?url:"";
            return {
                "background-image":'url("'+url+'"),url("")'
            }
        },
        //初始化数据
        init:function(){
            var that=this;
            that.shopData.forEach(function(val){
                if(val.parentId=="0"){
                    val.list=[];
                    that.areas.push(val);
                }
            });
            that.areas.forEach(function(val){
                that.shopData.forEach(function(key){
                    if(key.parentId==val.id){
                        val.list.push(key);
                    }
                });
                // that.idList1.push(val.id);
                // val.list.forEach(function(key){
                //     that.areasList.push(key.id);
                // });
            });
        },
        //添加商品规格
        getAddBtn:function(type,index,areafreightId){
            var that=this;
            console.log(index)
            console.log(areafreightId)
            that.areafreightId=areafreightId;
            if(type==1){
                that.list.subpiece.push("");
                that.list.submoney.push("");
                that.list.subaddPiece.push("");
                that.list.subaddMoney.push("");
                that.list.subprovinceId.push("");
                that.list.areafreightId.push("");
                that.formatNum++;
            }else if(type==2){
                if(areafreightId){
                    that.getIsClassify();
                }else{
                    that.list.subpiece.splice(index,1);
                    that.list.submoney.splice(index,1);
                    that.list.subaddPiece.splice(index,1);
                    that.list.subaddMoney.splice(index,1);
                    that.list.subprovinceId.splice(index,1);
                    that.list.areafreightId.splice(index,1);
                    that.formatNum--;
                }
            }
            this.areasList1=this.list.provinceId;
            this.list.subprovinceId.forEach(function(val){
                //console.log("--------所选中id---------")
                vm.areasList1=vm.areasList1+","+val;
            });
        },
        //选择地区
        getIsScreen:function(num,type){
            console.log(type+"-------------------")
            var that=this;
            that.isScreen=num;
            that.type=type;
            var urbanArea=$(".urbanArea").find(".urbanArea-list");
            urbanArea.removeClass("active");
            if(num==1){
                that.checkedNames=that.list.provinceId.split(",");
            }else if(num==2){
                if(that.list.subprovinceId[type].length>0){
                    that.checkedNames=that.list.subprovinceId[type].split(",");
                }else{
                    that.checkedNames=[];
                }
            }
            console.log("--------==========---------")
            that.optionalList=diff(that.areasList,that.areasList1.split(","));
            that.disableList=diff(that.areasList1.split(","),that.checkedNames);
            // console.log("--------所选中id---------")
            // console.log(vm.areasList1)
            // console.log("--------当前选中的id---------")
            // console.log(that.checkedNames)
            // console.log("--------可选id---------")
            // console.log(that.optionalList)
            // console.log("--------禁用id---------")
            // console.log(that.disableList)
            // console.log("--------==========---------")
            var flat,flat1;
            that.areas.forEach(function(val){
                val.list.forEach(function(key){
                    flat=false;
                    that.disableList.forEach(function(key1){
                        if(key.id==key1){
                            flat=true;
                        }
                    });
                    if(flat){
                        key.type=0;
                    }else{
                        key.type=1;
                    }
                    //that.areasList.push(key.id);
                });
            });
            // var urbanArea=$(".urbanArea").find(".urbanArea-list");
            that.getCheck();
            // that.$nextTick(function(){
            //     urbanArea.each(function(i,n){
            //         var listCent=$(n).find(".urbanArea-listCent");
            //         listCent.each(function(v,k){
            //             console.log("---------------")
            //             console.log("input个数"+$(k).find("input").length)
            //             console.log("禁用个数"+$(k).find("input:disabled").length)
            //             console.log("选中个数"+$(k).find("input:checked").length)
            //             var len=$(k).find("input").length;
            //             var len1=$(k).find("input:disabled").length;
            //             var len2=$(k).find("input:checked").length;
            //             if(len==len1){
            //                 //console.log("666")
            //                 $(this).siblings(".urbanArea-cent").find("label").addClass("addGoods-icon1");
            //             }else if(len!=len1&&len==len2){
            //                 //console.log("777")
            //
            //                 $(this).siblings(".urbanArea-cent").find("label").removeClass("addGoods-icon1").addClass("addGoods-icon2");
            //             }else{
            //                 //console.log("888")
            //             }
            //         });
            //     });
            // });

            // console.log("============不全选==========")
            // console.log(that.idList)
            // console.log("============不全选==========")
        },
        //下拉
        getlevel:function(index){
            var urbanArea=$(".urbanArea").find(".urbanArea-list");
            var list=urbanArea.eq(index);
            if(list.hasClass("active")){
                list.removeClass("active");
            }else{
                list.addClass("active");
            }
        },
        //全选、全部选
        checkedAll:function(index,id){
            console.log(id)
            var that=this;
            var d=$("#checkbox"+index);
            // console.log(d.is(':checked'))
            // console.log(that.checkedNames)
            // console.log("=======++========");
            // console.log(that.idList)
            // console.log("=======++========");
            if(d.hasClass("addGoods-icon2")){
                //console.log("111");
                d.removeClass("addGoods-icon2");
                that.areas.forEach(function(val){
                    if(val.id==id){
                        val.list.forEach(function(key){
                            that.checkedNames.splice($.inArray(key.id,that.checkedNames),1);
                        });
                    }
                });
            }else{
                console.log("222");
                d.addClass("addGoods-icon2");
                that.areas.forEach(function(val){
                    if(val.id==id){
                        val.list.forEach(function(key){
                            if($.inArray(key.id,that.checkedNames)==-1&&key.type!=0){
                                //console.log("333")
                                that.checkedNames.push(key.id);
                            }else{
                                //console.log('444')
                                //that.checkedNames.push(key.id);
                            }
                        });
                    }
                });

            }
            //that.getCheck();
        },
        //效果
        getCheck:function(){
            var that=this;
            var urbanArea=$(".urbanArea").find(".urbanArea-list");
            that.$nextTick(function(){
                urbanArea.each(function(i,n){
                    var listCent=$(n).find(".urbanArea-listCent");
                    listCent.each(function(v,k){
                        console.log("++++++++++++++++")
                        console.log("input个数"+$(k).find("input").length)
                        console.log("禁用个数"+$(k).find("input:disabled").length)
                        console.log("选中个数"+$(k).find("input:checked").length)
                        var len=$(k).find("input").length;
                        var len1=$(k).find("input:disabled").length;
                        var len2=$(k).find("input:checked").length;
                        var labe=$(this).siblings(".urbanArea-cent").find("label");
                        var inpu=$(this).siblings(".urbanArea-cent").find("input");
                        if(len==len2){
                            console.log("666")
                            labe.addClass("addGoods-icon2");
                        }else if(len1==len){
                            labe.removeClass("addGoods-icon2").addClass("addGoods-icon1");
                            console.log("7777")
                        }else if(len1==0&&len2==1){
                            console.log("555")
                            labe.removeClass("addGoods-icon2");
                        }else if(len1==1&&len2==0){
                            console.log("3333")
                            labe.removeClass("addGoods-icon2");
                        }else if(len1==0&&len2==0){
                            console.log("444")
                            labe.removeClass("addGoods-icon2");
                        }else if(len==len1){
                            console.log("888")
                            labe.addClass("addGoods-icon1");
                        }else if(len!=len1&&len==len2){
                            console.log("999")
                            labe.removeClass("addGoods-icon1").addClass("addGoods-icon2");
                        }else if((len1+len2)==len){
                            console.log("000")
                            labe.addClass("addGoods-icon2");
                        }else{
                            console.log("11111")
                        }


                    });
                });
            });
        },
        //确定
        getArea:function(){
            var that=this;
            if(that.isScreen==1&&that.type==0){
                that.list.provinceId=that.checkedNames.join(",");
                that.freightList.provinceName=that.checkedList.join(",");
            }else if(that.isScreen==2){
                that.list.subprovinceId[that.type]=that.checkedNames.join(",");
                $("#region"+that.type).html(that.checkedList.join(","));
            }
            // that.idList.push(that.checkedNames.join(","));
            //that.idList3[that.type]=that.idList.join(",");
            // console.log("-----------------idList3--------------")
            // console.log(that.idList3)
            // console.log("-----------------idList3--------------")
            that.checkedNames=[];
            //that.idList=[];
            that.getIsScreen();
        },
        //增加，修改运费模板
        saveProduct:function(){
            var that=this;
            console.log(that.list)
            console.log(JSON.stringify(that.list))
            // var subaddMoney=that.list.subaddMoney;
            // var a = [];
            // for(var i=0;i<subaddMoney.length;i++){
            //     var o = {};
            //    o.subaddMoney=subaddMoney[i] ;
            //     a.push(o)
            // }
            var data={
                loginUid:that.list.loginUid,
                name:encodeURI(that.list.name), //名称
                isExcept:0, //1除以下城市之外 0否
                provinceId :that.list.provinceId, //省份id 逗号隔开
                freightId:that.list.freightId, //运费id 没有值就是增加 有值就是修改
                piece:that.list.piece,  //件
                money:that.list.money,  //价格
                addPiece:that.list.addPiece,  //增加件
                addMoney :that.list.addMoney, //增加价格
                areafreightId:that.list.areafreightId.join(","), //子运费id数组
                subprovinceId:that.list.subprovinceId.join(","),  //省份id数组 逗号隔开
                subpiece:that.list.subpiece.join(","),  //件数组
                submoney:that.list.submoney.join(","),  //价格数组
                subaddPiece:that.list.subaddPiece.join(","), //增加件数组
                subaddMoney:that.list.subaddMoney.join(",")//增加价格数组
            };
            console.log(data)
            this.ajax({url:"/user/savefreight",data:data,dataType:"json",method:"post"},function(b){
                console.log(JSON.stringify(b))
                if(b.json.code==0){
                    console.log(b.json.data)
                    location.href="/freightTemplate";
                }else{
                    vm.$refs.prompt.toast("修改运费模板失败！")
                }
            });
        },
        numberOnly:function (item) {
            return item.replace(/[^\d]/g,'');
        }

    },
    watch: {
        checkedNames:function(){
            var that=this;
            that.checkedList=[];
            that.areas.forEach(function(val){
                val.list.forEach(function(key){
                    that.checkedNames.forEach(function(key1){
                        if(key1==key.id){
                            that.checkedList.push(key.name);
                        }
                    });
                });
            });
            // console.log("--------当前选中id---------")
            // console.log(that.checkedNames)
            // // that.idList.push(that.checkedNames.join(","));
            // console.log("--------当前选中id(城市名称)---------")
            // console.log(that.checkedList)
            this.areasList1=this.list.provinceId;
            this.list.subprovinceId.forEach(function(val){
                //console.log("--------所选中id---------")
                vm.areasList1=vm.areasList1+","+val;
            });
            // console.log("--------所选中id---------")
            // console.log(vm.areasList1.split(","))
            // console.log("--------所选中id---------")
            // console.log(vm.areasList1)
            // console.log(that.areas)
        },
        idList:function(){
            // console.log("583947598347593758930")
            // console.log("--------当前选中id---------")
            // console.log(this.idList)

            // this.idList3.forEach(function(val){
            //     //console.log("--------所选中id---------")
            //     vm.idList2=vm.idList2+","+val;
            // });
            if(this.idList3.length>0){
                vm.idList2=vm.idList2+this.idList
            }else{
                this.idList2=this.idList;
            }

            // console.log("--------所选中id---------")
            // console.log(this.idList2)
            // console.log(this.areas)
        }
        ,list:{
            handler:function(val, oldVal) {
                console.log(val)
                if(val.addMoney<0||val.addPiece<0||val.money<0||val.piece<0){
                    val.addMoney=Math.abs(val.addMoney);
                    val.addPiece=Math.abs(val.addPiece);
                    val.money=Math.abs(val.money);
                    val.piece=Math.abs(val.piece);
                }
                if(val.subaddMoney&&val.subaddPiece&&val.submoney&&val.subpiece){
                    for(var i=0;i<val.subaddMoney.length;i++){
                        val.subaddMoney[i]=Math.abs(val.subaddMoney[i]);
                        val.subaddPiece[i]=Math.abs(val.subaddPiece[i]);
                        val.submoney[i]=Math.abs(val.submoney[i]);
                        val.subpiece[i]=Math.abs(val.subpiece[i]);
                    }
                }
            },
            deep: true
        }
    }
});
function filterStr(str) {
    var pattern = new RegExp("[`~!@#$^&*()=|{}':;',\\[\\].<>/?~！@#￥……&*（）——|{}【】‘；：”“'。，、？%-_++_]");
    var specialStr = "";
    for(var i=0;i<str.length;i++){
        specialStr += str.substr(i,1).replace(pattern,"");
    }
    console.log(Math.abs(specialStr));
    return specialStr;
}
function diff(arr1, arr2) {
    return arr1.concat(arr2).filter(function(arg){
        return !(arr1.indexOf(arg) >= 0 && arr2.indexOf(arg) >= 0);
    });
}
function getSwitch(num,index){
    var d1=$("#checkbox"+index);
    var num2=vm.areas[index].list.length;
    console.log(num2)
    //console.log(d1)
    if(num==num2){
        d1.prop("checked",true);
    }else{
        d1.prop("checked",false);
        // if(d1.is(':checked')){
        // }
    }
}
// $(".urbanArea-more").on("click",function(){});